﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVType : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditKTVType()
        {
            InitializeComponent();
            InitDictItems();
        }

        private void InitDictItems()
        {
            this.txtCaculateType.Items.Clear();
            List<KTVCaculteTypeInfo> typeList = BLLFactory<KTVCaculteType>.Instance.GetAll();
            foreach (KTVCaculteTypeInfo typeInfo in typeList)
            {
                this.txtCaculateType.Items.Add(new CListItem(typeInfo.Name, typeInfo.ID.ToString()));
            }
            if (this.txtCaculateType.Items.Count > 0)
            {
                this.txtCaculateType.SelectedIndex = 0;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmEditRoomType_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                KTVSuiteTypeInfo info = BLLFactory<KTVSuiteType>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.txtPersons.Value = Convert.ToDecimal(info.Persons);
                    this.txtNote.Text = info.Note;
                    this.txtAtLeaseMoney.Text = info.AtLeaseMoney.ToString("f2");
                    this.txtName.Text = info.Name;
                    this.txtCaculateType.Text = info.CaculateType;
                    this.txtPrice.Text = info.Price.ToString("f2");
                    this.txtBuyOutPrice.Text = info.BuyOutPrice.ToString("f2");

                    this.txtCaculateType.Text = info.CaculateType;
                }
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Add");
            }
            this.txtName.Focus();
        }

        private void SetInfo(KTVSuiteTypeInfo info)
        {
            info.Persons = Convert.ToInt32(this.txtPersons.Value);
            info.AtLeaseMoney = Convert.ToDecimal(this.txtAtLeaseMoney.Text);
            info.Persons = Convert.ToInt32(this.txtPersons.Value);
            info.Name = this.txtName.Text.Trim();
            info.Note = this.txtNote.Text.Trim();
            info.Price = Convert.ToDecimal(this.txtPrice.Text);
            info.CaculateType = this.txtCaculateType.Text;
            info.BuyOutPrice = Convert.ToDecimal(this.txtBuyOutPrice.Text);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入房间类型");
                this.txtName.Focus();
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                KTVSuiteTypeInfo info = BLLFactory<KTVSuiteType>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<KTVSuiteType>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                KTVSuiteTypeInfo info = new KTVSuiteTypeInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<KTVSuiteType>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void txtCaculateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtCaculateType.SelectedItem as CListItem;
            if (item != null && !string.IsNullOrEmpty(item.Value))
            {
                KTVCaculteTypeInfo typeInfo = BLLFactory<KTVCaculteType>.Instance.FindByID(item.Value);
                if (typeInfo != null)
                {
                    this.txtPrice.Text = typeInfo.Price.ToString("f2");
                }
            }
        }
    }
}
